/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server.GUI;

import Server.BUS.CSCommunication;
import Server.DTO.Database;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Trung
 */
public class SocketForm extends javax.swing.JFrame{

    /**
     * Creates new form SocketForm
     */
    
    public static int PORT = -1;
    ServerSocket serverSK;
    Socket sk;
    Thread thread;
    List<Database> lstDB;
    
    public SocketForm() {
        initComponents();
    }   
    
    public SocketForm(List<Database> _lstDB)
    {
        initComponents();
        lstDB = _lstDB;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tf_port = new javax.swing.JTextField();
        bt_CreateSocket = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setText("Port:");

        tf_port.setText("3001");

        bt_CreateSocket.setText("Create Socket");
        bt_CreateSocket.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_CreateSocketActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(87, 87, 87)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(26, 26, 26)
                        .addComponent(tf_port, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(bt_CreateSocket)))
                .addContainerGap(100, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(82, 82, 82)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(tf_port, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(43, 43, 43)
                .addComponent(bt_CreateSocket)
                .addContainerGap(123, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_CreateSocketActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_CreateSocketActionPerformed
        // TODO add your handling code here:
        try {
            int port = Integer.parseInt(tf_port.getText());
            SocketForm.PORT = port;
//            serverSK = new ServerSocket(port);
//            JOptionPane.showMessageDialog(rootPane, "Server waiting clients to connect");
//            System.out.println("Server waiting clients to connect");
//            this.setVisible(false);
//            
//            sk = serverSK.accept();
//            JOptionPane.showMessageDialog(rootPane, "Server have connected client");
//            System.out.println("Server have connected client");
            thread = new Thread(new CSCommunication(PORT, lstDB));
            thread.start();
            this.setVisible(false);

//            InputStream is = sk.getInputStream();
//            OutputStream os = sk.getOutputStream();
//            BufferedReader br = new BufferedReader(new InputStreamReader(is));
//            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
//            ObjectOutputStream oos = new ObjectOutputStream(os);

        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        } catch (NumberFormatException nex) {
            JOptionPane.showMessageDialog(rootPane, "Invalid Port! Please input again");
        }
    }//GEN-LAST:event_bt_CreateSocketActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SocketForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SocketForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SocketForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SocketForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SocketForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_CreateSocket;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField tf_port;
    // End of variables declaration//GEN-END:variables
}
